package org.restoran.util;

import org.restoran.model.Order;
import org.restoran.model.Table;
import org.restoran.repository.TableRepository;
import org.restoran.service.ServiceException;

import java.util.Objects;

/**
 * @author Snisar Bogdan.
 */
public class Guards {


    public static class OrderServiceGuard {

        public static void checkCondition(boolean condition, String massage) {
            if(!condition) throw new ServiceException(massage);
        }

        public static void checkTableInOrder(Order order, TableRepository tableRepository)
        {
            Objects.requireNonNull(tableRepository);
            Objects.requireNonNull(order);

            Table table = order.getTable();
            OrderServiceGuard.checkCondition(table != null, "creating new Order without given \"tableId\"");

            Integer tableId = table.getId();
            boolean exists = tableRepository.exists(tableId);

            OrderServiceGuard.checkCondition(exists, "not real table exists on this tableId");
        }


    }

}
